package com.ruoyi.carrepair.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.carrepair.domain.QxOperatelog;
import com.ruoyi.carrepair.mapper.QxOperatelogMapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.zjc.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.carrepair.mapper.QxBxgsMapper;
import com.ruoyi.carrepair.domain.QxBxgs;
import com.ruoyi.carrepair.service.IQxBxgsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 保险公司管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-17
 */
@Service
public class QxBxgsServiceImpl implements IQxBxgsService 
{
    @Autowired
    private QxBxgsMapper qxBxgsMapper;

    @Autowired
    private QxOperatelogMapper qxOperatelogMapper;

    /**
     * 查询保险公司管理
     * 
     * @param id 保险公司管理主键
     * @return 保险公司管理
     */
    @Override
    public QxBxgs selectQxBxgsById(String id)
    {
        return qxBxgsMapper.selectQxBxgsById(id);
    }

    /**
     * 查询保险公司管理列表
     * 
     * @param qxBxgs 保险公司管理
     * @return 保险公司管理
     */
    @Override
    public List<QxBxgs> selectQxBxgsList(QxBxgs qxBxgs)
    {
        qxBxgs.setToApplet(SecurityUtils.getLoginUser().getToApplet());
        return qxBxgsMapper.selectQxBxgsList(qxBxgs);
    }

    /**
     * 新增保险公司管理
     * 
     * @param qxBxgs 保险公司管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertQxBxgs(QxBxgs qxBxgs)
    {
        String toapplet = SecurityUtils.getLoginUser().getToApplet();
        Long createUserId = SecurityUtils.getLoginUser().getUserId();
        String createUserName = SecurityUtils.getLoginUser().getUsername();

        qxBxgs.setToApplet(SecurityUtils.getLoginUser().getToApplet());
        qxBxgs.setCreateID(SecurityUtils.getLoginUser().getUserId());
        qxBxgs.setCreator(SecurityUtils.getLoginUser().getUsername());
        qxBxgs.setCreateDate(new Date());
        qxBxgs.setRowVersion(0L);
        qxBxgs.setIsDeleted(0L);
        qxBxgs.setId(String.valueOf(new SnowflakeIdWorker(5,8).nextId()));
        int num = qxBxgsMapper.insertQxBxgs(qxBxgs);

        QxOperatelog qxOperatelog = new QxOperatelog();
        qxOperatelog.setId(String.valueOf(new SnowflakeIdWorker(5, 8).nextId()));
        qxOperatelog.setContent(createUserName + "在" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "新增了一条保险公司信息：【" + qxBxgs.getMingcheng() + "】");
        qxOperatelog.setCategory("1");
        qxOperatelog.setMenuModel("保险公司管理");
        qxOperatelog.setCreateID(createUserId);
        qxOperatelog.setCreator(createUserName);
        qxOperatelog.setToApplet(toapplet);
        qxOperatelog.setCreateDate(new Date());
        qxOperatelogMapper.insertQxOperatelog(qxOperatelog);

        return num;
    }

    /**
     * 修改保险公司管理
     * 
     * @param qxBxgs 保险公司管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateQxBxgs(QxBxgs qxBxgs)
    {
        String toapplet = SecurityUtils.getLoginUser().getToApplet();
        Long createUserId = SecurityUtils.getLoginUser().getUserId();
        String createUserName = SecurityUtils.getLoginUser().getUsername();

        int num = qxBxgsMapper.updateQxBxgs(qxBxgs);

        QxOperatelog qxOperatelog = new QxOperatelog();
        qxOperatelog.setId(String.valueOf(new SnowflakeIdWorker(5, 8).nextId()));
        qxOperatelog.setContent(createUserName + "在" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "修改了一条保险公司信息：【" + qxBxgs.getMingcheng() + "】");
        qxOperatelog.setCategory("2");
        qxOperatelog.setMenuModel("保险公司管理");
        qxOperatelog.setCreateID(createUserId);
        qxOperatelog.setCreator(createUserName);
        qxOperatelog.setToApplet(toapplet);
        qxOperatelog.setCreateDate(new Date());
        qxOperatelogMapper.insertQxOperatelog(qxOperatelog);

        return num;
    }

    /**
     * 批量删除保险公司管理
     * 
     * @param ids 需要删除的保险公司管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteQxBxgsByIds(String[] ids)
    {
        String idsStr="";
        for(String id:ids){
            idsStr = idsStr + id + ",";
        }
        String toApplet = SecurityUtils.getLoginUser().getToApplet();
        Long createID = SecurityUtils.getLoginUser().getUserId();
        String creator = SecurityUtils.getLoginUser().getUsername();
        int num = qxBxgsMapper.deleteUpdQxBxgsByIds(ids);
        QxOperatelog qxOperatelog = new QxOperatelog();
        qxOperatelog.setId(String.valueOf(new SnowflakeIdWorker(5, 8).nextId()));
        qxOperatelog.setContent(creator + "在" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "删除了保险公司信息。保险公司id：【" + idsStr+ "】");
        qxOperatelog.setCategory("3");
        qxOperatelog.setMenuModel("保险公司管理");
        qxOperatelog.setCreateID(createID);
        qxOperatelog.setCreator(creator);
        qxOperatelog.setToApplet(toApplet);
        qxOperatelog.setCreateDate(new Date());
        qxOperatelogMapper.insertQxOperatelog(qxOperatelog);

        return num;
    }

    /**
     * 删除保险公司管理信息
     * 
     * @param id 保险公司管理主键
     * @return 结果
     */
    @Override
    public int deleteQxBxgsById(String id)
    {
        return qxBxgsMapper.deleteQxBxgsById(id);
    }

}
